import {
  schoolCourseLsit
} from '@/api/course/course'
import {
  deleteCourseById
} from '@/api/manage/index.js'
import {
  get_img_src
} from '@/utils/index.js'
import CourseSection from '@/components/Course/CourseSection/index.vue'
export default {
  components: {
    CourseSection
  },
  data() {
    return {
      tabbarActive: '2',
      tabbar: {
        options: [{
          name: '2',
          title: '公开课程'
        },
        {
          name: '1',
          title: '本校课程'
        }
        ]
      },
      listParam: { // 查询条件
        sign: '2', // 课程查询标记： 1:本校；2:公开；3可购买；4已购买
        learningPhaseId: null, // 学段ID
        courseGradeId: null, // 年级ID
        courseSubjectId: null, // 学科ID
        courseVersionId: null, // /版本ID
        courseVolumeId: null, // 册别ID
        page: 1, // 第几页
        size: 5 // 每页查询记录条数
      },
      listDatas: [],
      listPages: {
        layout: 'total, sizes, prev, pager, next, jumper', // 分页显示参数
        page: 1, // 当前所在分页数
        size: 5, // 每页显示的条目数
        sizes: [5, 10, 15, 20], // 每页显示的条目数设置
        total: 0, // 总条数
        count: 5, // 总页数=总条数/每页条目
        counter: 7, // 页码按钮的数量，当总页数超过该值时会折叠
        prevtext: '上一页',
        nexttext: '下一页',
        single: false
      }
    }
  },
  created() {
    this.schoolCourseLsit()
  },
  watch: {
    $route: {
      handler() {
        this.listParam.learningPhaseId = this.$route.query.learningPhaseId || null
        this.listParam.courseGradeId = this.$route.query.courseGradeId || null
        this.listParam.courseSubjectId = this.$route.query.courseSubjectId || null
        this.listParam.courseVersionId = this.$route.query.courseVersionId || null
        this.listParam.courseVolumeId = this.$route.query.courseVolumeId || null
      },
      deep: true
    },
    tabbarActive: {
      handler() {
        this.listParam.sign = this.tabbarActive
      },
      deep: true
    },
    listParam: {
      handler() {
        this.schoolCourseLsit()
      },
      deep: true
    }
  },
  methods: {
    schoolCourseLsit() { /** 查询课程列表 */
      this.listParam.page = this.listPages.page
      this.listParam.size = this.listPages.size
      this.loadObj = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      schoolCourseLsit(this.listParam).then(response => {
        // console.log(response.data)
        this.listDatas = response.data
        this.listPages.total = Number(response.total)
        this.loadObj.close()
      })
    },
    courseSectionBack(listParam) {
      this.listParam = listParam
      this.listParam.sign = this.tabbarActive
      this.schoolCourseLsit()
      // console.log(listParam)
    },
    clickTo(item) {
      this.$router.push({
        path: 'step1',
        query: {
          courseId: item.courseId,
          title: item.courseName
        }
      })
    },
    page_size_change(size) { // 改变每页条数
      this.listParam.size = size
      this.listPages.size = size
    },
    page_current_change(page) { // 点击当前页面
      this.listParam.page = page
      this.listPages.page = page
    },
    page_prev_click() { // 点击上一页
      this.listPages.page--
      this.listParam.page = this.listPages.page
    },
    page_next_click() { // 点击下一页
      this.listPages.page++
      this.listParam.page = this.listPages.page
    },
    simulateData() {

    },
    /** 获取文件路径 */
    get_img_src(val) {
      return get_img_src(val)
    },
    detailRow(item) {
      this.$router.push({
        path: '/courseware/detail',
        query: {
          courseId: item.courseId,
          courseName: item.courseName
        }
      })
    },
    editRow(item) {
      this.$router.push({
        path: '/courseware/edit',
        query: {
          courseId: item.courseId,
          act: 'edit',
          stepActive: 1
        }
      })
    },
    deleteRow(item, i) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCourseById({
          courseId: item.courseId
        }).then(response => {
          if (response.code === 200 || response.code === 20000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            location.reload()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
